package com.zhijiang.hospital.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.tedu.store.config.util.HttpUtil;
import com.zhijiang.common.util.MassageUtil;
import com.zhijiang.hospital.entrty.*;
import com.zhijiang.hospital.entrty.dto.EmployeeDto;
import com.zhijiang.hospital.entrty.dto.MeetingEmployeeDto;
import com.zhijiang.hospital.mapper.MeetingCheckRecodeMapper;
import com.zhijiang.hospital.mapper.MeetingEmployeeMapper;
import com.zhijiang.hospital.mapper.MeetingMapper;
import com.zhijiang.hospital.service.MeetingEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class MeetingEmployeeServiceImpl implements MeetingEmployeeService {
    @Autowired
    MeetingEmployeeMapper meetingEmployeeMapper;
    @Autowired
    MeetingMapper meetingMapper;

    @Override
    public String insertEmployee(List<MeetingEmployeeDto> dto, String mCode, String mName, Date mBeginDate,Date mEndDate, String mAddress) throws ParseException {



        for (MeetingEmployeeDto meetingEmployeeDto : dto) {
            if (Objects.isNull(meetingEmployeeDto.getComEmployees())){

                SMeetingcheckrecode one = meetingEmployeeMapper.getOne(mCode,meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());
                System.out.println(one);
                ComEmployee c = meetingEmployeeMapper.getPhone(meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());
                if(Objects.isNull(one) ){
                    if(Objects.isNull(c)){
                        throw new RuntimeException("不是本院职工,无法添加");
                    }
                    MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
                    meetingCheckRecode.setMeetingCode(mCode);
                    meetingCheckRecode.setEmpCode(meetingEmployeeDto.getId());
                    meetingCheckRecode.setEmpName(meetingEmployeeDto.getLabel());
                    meetingCheckRecode.setEmpPhone(c.getRemark());
                    meetingCheckRecode.setEmpBeginDate(new Date());
                    //meetingCheckRecode.setPBeginDate(new Date());
                    meetingCheckRecode.setEmpDeptCode(c.getDeptCode());
                    Date date = new Date();
                    Date parse = DateUtil.parse(date.toString());
                    meetingCheckRecode.setEmpBeginDate(parse);
                    meetingCheckRecode.setEmpDeptName(meetingEmployeeDto.getDeptName());
                    meetingCheckRecode.setEmpMeetingvalid("0");
                    meetingCheckRecode.setMeetingName(mName);
                    meetingCheckRecode.setPAddress(mAddress);
                    //查询科室   设置科室
                    meetingCheckRecode.setMAddress(mAddress);
                    MeetingEmployeeDto dto1    = meetingEmployeeMapper.getByName(c.getDeptCode());
                   meetingCheckRecode.setEmpDeptName(dto1.getDeptName());
                    meetingCheckRecode.setMeetingBeginDate(null);
                    meetingCheckRecode.setPAddress(" ");
                    meetingCheckRecode.setEmpRemark1("");
                    meetingCheckRecode.setEmpRemark2("");
                    meetingCheckRecode.setEmpRemark3("职工");
                    Integer integer = meetingEmployeeMapper.insert(meetingCheckRecode);
                }else if (one.getPValidity().equals("4")){
                    String code = "0";
                    meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                }
            }else {
                for (ComEmployee comEmployee : meetingEmployeeDto.getComEmployees()) {
                    SMeetingcheckrecode sMeetingcheckrecode1 = meetingEmployeeMapper.getOne(mCode,comEmployee.getEmplCode(), comEmployee.getEmplName());
                    ComEmployee c = meetingEmployeeMapper.getPhone(comEmployee.getEmplCode(), comEmployee.getEmplName());

                    if (Objects.isNull(sMeetingcheckrecode1)){


                        log.info(comEmployee.toString());

                        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();

                        if(Objects.isNull(c)) {
                            throw new RuntimeException("该用户不存在");
                        }

                        meetingCheckRecode.setMeetingCode(mCode);
                        meetingCheckRecode.setEmpCode(c.getEmplCode());
                        meetingCheckRecode.setEmpName(c.getEmplName());
                        //REMARK数据库里面的字段代表手机号码
                        MeetingEmployeeDto dto1    = meetingEmployeeMapper.getByName(c.getDeptCode());
                        meetingCheckRecode.setEmpPhone(c.getRemark());
                        meetingCheckRecode.setEmpBeginDate(new Date());
                        //meetingCheckRecode.setPBeginDate(new Date());
                        meetingCheckRecode.setEmpDeptCode(c.getDeptCode());
                        Date date = new Date();
                        Date parse = DateUtil.parse(date.toString());
                        meetingCheckRecode.setEmpBeginDate(parse);
                        meetingCheckRecode.setEmpDeptName(dto1.getDeptName());
                        meetingCheckRecode.setEmpMeetingvalid("0");
                        meetingCheckRecode.setMeetingName(mName);
                        meetingCheckRecode.setMAddress(mAddress);
                        meetingCheckRecode.setPAddress(mAddress);
                        meetingCheckRecode.setMeetingBeginDate(null);
                        meetingCheckRecode.setPAddress("1");
                        meetingCheckRecode.setEmpRemark1("");
                        meetingCheckRecode.setEmpRemark2("");
                        meetingCheckRecode.setEmpRemark3("职工");
                        Integer integer = meetingEmployeeMapper.insert(meetingCheckRecode);





                    }else if (sMeetingcheckrecode1.equals("4")){
                        String code = "0";
                        meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                    }
                }
            }

        }

        return "人员创建成功";
    }
    @Autowired
    MeetingCheckRecodeMapper meetingCheckRecodeMapper;
    @Override
    public void addList(String mCode, List<MeetingEmployeeDto> dto) {
        Meeting meeting = meetingMapper.getMeeting(mCode);
        for (MeetingEmployeeDto meetingEmployeeDto : dto) {
                if (Objects.isNull(meetingEmployeeDto.getComEmployees())){

                    SMeetingcheckrecode one = meetingEmployeeMapper.getOne(mCode,meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());
                    System.out.println(one);
                    ComEmployee c = meetingEmployeeMapper.getPhone(meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());

                    if(Objects.isNull(one) ){
//                        if(Objects.isNull(c)){
//                                throw new RuntimeException("不是本院职工,无法添加");
//                        }
                        MeetingEmployeeDto dto1    = meetingEmployeeMapper.getByName(c.getDeptCode());
                        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
                        meetingCheckRecode.setMeetingCode(mCode);
                        meetingCheckRecode.setEmpCode(meetingEmployeeDto.getId());
                        meetingCheckRecode.setEmpName(meetingEmployeeDto.getLabel());
                        meetingCheckRecode.setEmpPhone(c.getRemark());
                        meetingCheckRecode.setEmpBeginDate(new Date());
                        //meetingCheckRecode.setPBeginDate(new Date());
                        meetingCheckRecode.setEmpDeptCode(dto1.getDeptCode());
                        Date date = new Date();
                        Date parse = DateUtil.parse(date.toString());
                        meetingCheckRecode.setEmpBeginDate(parse);
                        meetingCheckRecode.setEmpDeptName(dto1.getDeptName());
                        meetingCheckRecode.setEmpMeetingvalid("0");
                        meetingCheckRecode.setMeetingName(meeting.getMName());
                        meetingCheckRecode.setPAddress(meeting.getMAddress());
                        meetingCheckRecode.setMeetingBeginDate(null);
                        meetingCheckRecode.setPAddress(" ");
                        meetingCheckRecode.setEmpRemark1("");
                        meetingCheckRecode.setEmpRemark2("");
                        meetingCheckRecode.setEmpRemark3("职工");
                        meetingCheckRecode.setMAddress(meeting.getMAddress());
                        Integer integer = meetingEmployeeMapper.insert(meetingCheckRecode);
                    }else if (one.getPValidity().equals("4")){
                       String code = "0";
                        meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                    }
                }else {
                    for (ComEmployee comEmployee : meetingEmployeeDto.getComEmployees()) {
                        SMeetingcheckrecode sMeetingcheckrecode1 = meetingEmployeeMapper.getOne(mCode,comEmployee.getEmplCode(), comEmployee.getEmplName());
                        ComEmployee c = meetingEmployeeMapper.getPhone(comEmployee.getEmplCode(), comEmployee.getEmplName());

                        if (Objects.isNull(sMeetingcheckrecode1)){


                            log.info(comEmployee.toString());

                            MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();

                            if(Objects.isNull(c)) {
                                throw new RuntimeException("该用户不存在");
                            }
                            MeetingEmployeeDto dto1    = meetingEmployeeMapper.getByName(c.getDeptCode());

                            meetingCheckRecode.setMeetingCode(mCode);
                            meetingCheckRecode.setEmpCode(c.getEmplCode());
                            meetingCheckRecode.setEmpName(c.getEmplName());
                            //REMARK数据库里面的字段代表手机号码
                            meetingCheckRecode.setPAddress(meeting.getMAddress());
                            meetingCheckRecode.setEmpPhone(c.getRemark());
                            meetingCheckRecode.setEmpBeginDate(new Date());
                            //meetingCheckRecode.setPBeginDate(new Date());
                            meetingCheckRecode.setEmpDeptCode(dto1.getDeptCode());
                            Date date = new Date();
                            Date parse = DateUtil.parse(date.toString());
                            meetingCheckRecode.setEmpBeginDate(parse);
                            meetingCheckRecode.setEmpDeptName(dto1.getDeptName());
                            meetingCheckRecode.setEmpMeetingvalid("0");
                            meetingCheckRecode.setMeetingName(meeting.getMName());
                            meetingCheckRecode.setMAddress(meeting.getMAddress());
                            meetingCheckRecode.setMeetingBeginDate(null);
                            meetingCheckRecode.setPAddress("1");
                            meetingCheckRecode.setEmpRemark1("");
                            meetingCheckRecode.setEmpRemark2("");
                            meetingCheckRecode.setEmpRemark3("职工");
                            Integer integer = meetingEmployeeMapper.insert(meetingCheckRecode);





                        }else if (sMeetingcheckrecode1.getPValidity().equals("4")){
                            String code = "0";
                            meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                        }
                    }
                }

            }


    }

    @Override
    public void deleteGroup(String mCode, List<MeetingEmployeeDto> dto) {

        for (MeetingEmployeeDto meetingEmployeeDto : dto) {
            //判断这个组有没有人没有人从科室级找
            if (Objects.isNull(meetingEmployeeDto.getComEmployees())){
                //查询是否在本次会议中
                SMeetingcheckrecode one = meetingEmployeeMapper.getOne(mCode,meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());
                System.out.println(one);
                ComEmployee c = meetingEmployeeMapper.getPhone(meetingEmployeeDto.getId(), meetingEmployeeDto.getLabel());
                if(!Objects.isNull(one)||!one.getPValidity().equals("4")){
                    if (!Objects.isNull(c)){
                        String code = "4";
                        meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                    }
                }
            }else {

                for (ComEmployee comEmployee : meetingEmployeeDto.getComEmployees()) {
                    //查询这个人在删除
                    SMeetingcheckrecode sMeetingcheckrecode1 = meetingEmployeeMapper.getOne(mCode,comEmployee.getEmplCode(), comEmployee.getEmplName());
                    ComEmployee c = meetingEmployeeMapper.getPhone(comEmployee.getEmplCode(), comEmployee.getEmplName());

                    if(!Objects.isNull(sMeetingcheckrecode1)){
                        if (!Objects.isNull(c)){
                            String code = "4";
                            meetingCheckRecodeMapper.delete(mCode,c.getRemark(),code);
                        }
                    }

                }



            }




        }

    }

    //短信发送
    @Override
    public Meeting send(String mCode, String iphone) {
        HospitalSmsDto smsDto = new HospitalSmsDto();
           Meeting meeting = meetingMapper.getMeeting(mCode);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        smsDto.setPhone(iphone);
        SMeetingcheckrecode sMeetingcheckrecode =  meetingEmployeeMapper.getName(mCode,iphone);
//        if (sMeetingcheckrecode.getPRemark3().equals("职工")){
            smsDto.setContent("你好！"+sMeetingcheckrecode.getPName()+"先生你有场会议于"+simpleDateFormat.format(meeting.getMBeginDate())
                    +"位置在"+meeting.getMAddress()+"请尽快到场扫码签到。");
            MassageUtil.smsUtil(smsDto);
            sendPhone(simpleDateFormat.format(meeting.getMBeginDate()),meeting.getMAddress(),sMeetingcheckrecode.getPPhone());
            return meeting;


//        }
//        return meeting;
    }

    private void sendPhone(String date2, String mAddress, String pPhone) {



    }

    @Override
    public void sendAll(String mCode, List<SMeetingcheckrecode> dto) {

        Meeting meeting = meetingMapper.getMeeting(mCode);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        for (SMeetingcheckrecode sMeetingcheckrecode : dto) {
            HospitalSmsDto smsDto = new HospitalSmsDto();
            smsDto.setContent("你好！"+sMeetingcheckrecode.getPName()+"先生你有场会议于"+simpleDateFormat.format(meeting.getMBeginDate())
                    +"位置在"+meeting.getMAddress()+"请尽快到场扫码签到。");
            MassageUtil.smsUtil(smsDto);

        }
    }


}

